package com.hzjh.base.style.view.loading;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.hzjh.base.log.JHLogger;
import com.hzjh.base.style.R;


/**
 * 对话框 Loading
 */
public class LoadingDialogManager {

    private static LoadingDialogManager ourInstance = new LoadingDialogManager();
    private Dialog mDialog;
    private Dialog mProgressDialog;

    public static LoadingDialogManager get() {
        return ourInstance;
    }

    private LoadingDialogManager() {
    }

    public boolean isShowing() {
        if (mDialog == null) {
            return false;
        }
        return mDialog.isShowing();
    }

    /**
     * 展示dialog
     */
    public void showLoading(Activity activity) {
        try {
            if (mDialog != null && mDialog.isShowing()) {
                return;
            }
            if (activity instanceof LifecycleOwner) {
                Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle();
                Lifecycle.State state = lifecycle.getCurrentState();
                lifecycle.addObserver(new LifecycleEventObserver() {
                    @Override
                    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                        lifecycle.removeObserver(this);
                        if (event == Lifecycle.Event.ON_DESTROY) {
                            mDialog.dismiss();
                            mDialog = null;
                        }
                    }
                });
                if (state == Lifecycle.State.DESTROYED) {
                    return;
                }
            }

            mDialog = new AppCompatDialog(activity, R.style.Theme_LoadingDialog);
            //自定义布局
            View view = LayoutInflater.from(activity).inflate(R.layout.base_layout_loading, null);
            mDialog.setContentView(view);
            //一定要先show出来再设置dialog的参数，不然就不会改变dialog的大小了
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(false);

            Window window = mDialog.getWindow(); //得到对话框
            window.setWindowAnimations(R.style.loading_dialog_anim); //设置窗口弹出动画

            mDialog.setOnCancelListener(dialog -> {
                if (mListener != null) {
                    mListener.onLoadingCancel();
                }
            });

            mDialog.show();
            // TODO fix, each activity own a dialog
        } catch (Error error) {
            JHLogger.e("LoadingDialogManager", error.toString());
        }
    }



    /**
     * 隐藏dialog
     */
    public void dismissLoading() {
//        SKLogger.e("dss", "dismissLoading");
        try {
            if (mDialog != null && mDialog.isShowing()) {
                mDialog.dismiss();
                mDialog.cancel();
            }
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
                mProgressDialog.cancel();
            }
            // TODO fix, each activity own a dialog
        } catch (Throwable e) {


        }
    }

    private OnLoadingCancelListener mListener;

    public void SetOnLoadingCancelListener(OnLoadingCancelListener listener) {
        this.mListener = listener;
    }

    public interface OnLoadingCancelListener {

        void onLoadingCancel();
    }

}
